<script setup lang="ts">
import { ref, watch } from 'vue'
import { reqHospitalWork, reqHospitalDoctor } from '@/api/hospitalsInfo.ts'
import { useRouter } from 'vue-router';
const router = useRouter();
import type { HospitalWordData, aaa, WorkData, BaseMap, Doctor } from '@/api/type.ts'
let pageNum = ref<number>(1)
let pageSize = ref<number>(6)
const workSchedule = ref({} as aaa)
const chooseSchedule = ref()
// 获取某医院某科室挂号情况
const getWorkSchedule = async () => {
    const res = await reqHospitalWork(pageNum.value, pageSize.value, router.currentRoute.value.query?.hoscode as string, router.currentRoute.value.query?.depcode as string).then(res => {
        workSchedule.value = res.data
        // console.log(res)
        // 默认第一个为选中
        chooseSchedule.value = workSchedule.value.bookingScheduleList[0]
    })
}
getWorkSchedule()
const handleSizeChange = (val: number) => {
    pageSize.value = val
    getWorkSchedule()
}
const handleCurrentChange = (val: number) => {
    pageNum.value = val
    getWorkSchedule()
}

const docArr = ref<Doctor[]>([])
const morningArr = ref<Doctor[]>([])
const afternoonArr = ref<Doctor[]>([])
watch(chooseSchedule, async () => {
    const res: any = await reqHospitalDoctor(router.currentRoute.value.query?.hoscode as string, router.currentRoute.value.query?.depcode as string, chooseSchedule.value.workDate)
    // console.log(res)
    docArr.value = res.data
    morningArr.value = docArr.value.filter((item) => item.workTime == 0)
    afternoonArr.value = docArr.value.filter((item) => item.workTime == 1)
}, {
    deep: true
})

//路由跳转进入到选择就诊人页面
const goStep2 = (doctor: any) => {
    //编程式导航进行路由跳转且携带医生的ID
    router.push({ path: "/hospital/register_step2", query: { docId: doctor.id } });
};
</script>

<template>
    <el-breadcrumb separator="/">
        <el-breadcrumb-item>{{ workSchedule?.baseMap?.hosname }}</el-breadcrumb-item>
        <el-breadcrumb-item>{{ workSchedule?.baseMap?.bigname }}</el-breadcrumb-item>
        <el-breadcrumb-item>{{ workSchedule?.baseMap?.depname }}</el-breadcrumb-item>
    </el-breadcrumb>
    <h3 class="schedule-title">{{ workSchedule?.baseMap?.workDateString }}</h3>
    <div class="schedule-list">
        <template v-if="workSchedule?.total > 0">
            <div class="schedule-item" v-for="(item, index) in workSchedule?.bookingScheduleList" :key="index"
                :id="item.workDate" :class="{ activeSchedule: item.workDate == chooseSchedule?.workDate }"
                @click="chooseSchedule = item">
                <h4>{{ item.workDate + '-' + item.dayOfWeek }}</h4>
                <span>{{ item.status == -1 ? '停止挂号' : (item.status == 0 ? item.availableNumber == -1 ? "约满了" :
            `有号(${item.availableNumber})` : '即将放号') }}</span>
            </div>
        </template>

    </div>
    <el-pagination layout="prev, pager, next" :total="workSchedule.total" v-model:current-page="pageNum"
        v-model:page-size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <p v-if="chooseSchedule?.status == 1"
        style="text-align: center;margin: 20px 0;font-size: 16px;color: var(--theme-color);">即将放号,每天{{
            workSchedule?.baseMap?.releaseTime }}放号
    </p>
    <!-- 医生 -->
    <div v-else>
        <!-- 上午 -->
        <div class="morning">
            <!-- 顶部文字提示 -->
            <div class="tip">
                <span class="text">上午号源</span>
            </div>
            <!--每一个医生的信息-->
            <div class="doc_info" v-for="doctor in morningArr" :key="doctor.id">
                <!-- 展示医生的名字|技能 -->
                <div class="left">
                    <div class="info">
                        <span>{{ doctor.title }}</span>
                        <span>|</span>
                        <span>{{ doctor.docname }}</span>
                    </div>
                    <div class="skill">{{ doctor.skill }}</div>
                </div>
                <!-- 右侧区域展示挂号的钱数-->
                <div class="right">
                    <div class="money">￥{{ doctor.amount }}</div>
                    <el-button type="primary" size="default" @click="goStep2(doctor)">{{
            doctor.availableNumber
        }}</el-button>
                </div>
            </div>
        </div>
        <!-- 下午 -->
        <div class="afternoon">
            <div class="tip">
                <span class="text">下午号源</span>
            </div>
            <!--每一个医生的信息-->
            <div class="doc_info" v-for="doctor in afternoonArr" :key="doctor.id">
                <!-- 展示医生的名字|技能 -->
                <div class="left">
                    <div class="info">
                        <span>{{ doctor.title }}</span>
                        <span>|</span>
                        <span>{{ doctor.docname }}</span>
                    </div>
                    <div class="skill">{{ doctor.skill }}</div>
                </div>
                <!-- 右侧区域展示挂号的钱数-->
                <div class="right">
                    <div class="money">￥{{ doctor.amount }}</div>
                    <el-button type="primary" size="default" @click="goStep2(doctor)">{{
            doctor.availableNumber
                        }}</el-button>
                </div>
            </div>
        </div>

    </div>
    <el-button type="primary" style="margin: 20px;"
        @click="goStep2({ id: 123456 })">接口内无医生数据，所以暂时无法完成预约并跳转，这里先展示一个按钮，模拟选中医生后跳转动作</el-button>

</template>

<style scoped lang="scss">
.el-breadcrumb {
    margin-top: 20px;
    font-size: 1rem;
}

.schedule-title {
    font-size: 1.5rem;
    font-weight: 600;
    text-align: center;
    margin: 20px 0;
}

.schedule-list {
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: space-around;
    align-items: center;

    .schedule-item {
        width: 14%;
        height: 90px;
        border: 1px solid var(--border-color);
        display: flex;
        flex-direction: column;
        transition: all 0.3s linear;

        h4 {
            font-size: 1rem;
            height: 30px;
            line-height: 30px;
            text-align: center;
            background-color: var(--gray-color1);
        }

        span {
            flex: 1;
            text-align: center;
            padding: 20px 0;
        }
    }

    .activeSchedule {
        width: 16%;
        height: 100%;

    }
}

.el-pagination {
    margin-top: 20px;
    justify-content: center;
}

.morning,
.afternoon {
    .tip {
        display: flex;
        align-items: center;

        .text {
            color: #7f7f7f;
            font-weight: 900;
        }
    }

    .doc_info {
        display: flex;
        justify-content: space-between;
        margin: 10px 0px;
        border-bottom: 1px solid #ccc;

        .left {
            .info {
                color: skyblue;
                margin: 10px 0px;

                span {
                    margin: 0px 5px;
                    font-size: 18px;
                    font-weight: 900;
                }
            }

            .skill {
                margin: 10px 0px;
                color: #7f7f7f;
            }
        }

        .right {
            width: 150px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .money {
                color: #7f7f7f;
                font-weight: 900;
            }
        }
    }
}
</style>